package july.framework.exsolver.reader;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import july.framework.exsolver.bean.Mapper;
import july.framework.exsolver.bean.Resolver;
import july.framework.exsolver.factory.ResolverFactory;
import july.framework.exsolver.factory.impl.DefaultResolverFactory;
import july.framework.exsolver.handler.ResolversHandler;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.text.Normalizer;

@Getter
public class ResolverReader {

    private static ResolverReader instance = new ResolverReader();

    @Autowired
    private ReadConfig readConfig;

    private String dirPath = "classpath:/exsolverMapper";

    private ResolverFactory resolverFactory;

    private ResolverReader(){

    }

    public static ResolverReader getInstance(){
        return instance;
    }

    /**
     * 读取配置
     */
    public void read(){
        if (readConfig == null){
        }

        if (StringUtils.isEmpty(dirPath)){
            return;
        }

        if (dirPath.contains("classpath:")){
            dirPath = dirPath.replace("classpath:",ResolverReader.class.getResource("/").getPath());
            dirPath = Normalizer.normalize(dirPath,Normalizer.Form.NFKC);
        }

        File dir = new File(dirPath);
        if (!dir.exists() || !dir.isDirectory()){
            return;
        }

        // 处理文件,读取Resolver
        for(File f : dir.listFiles()){
            FileReader fileReader = null;
            try {
                fileReader = new FileReader(f);
                StringBuilder jsonBuilder = new StringBuilder();
                int num;
                char[] buffer = new char[100];
                while ((num = fileReader.read(buffer)) != -1){
                    jsonBuilder.append(buffer,0,num);
                }

                String json = jsonBuilder.toString();
                Mapper mapper = JSON.parseObject(json, Mapper.class);

                if (mapper == null){
                    return;
                }

                String name = mapper.getName();

                if (resolverFactory == null){
                    resolverFactory = new DefaultResolverFactory();
                }

                for (String resolverStr :  mapper.getResolvers()){
                    JSONObject jso = JSON.parseObject(resolverStr);
                    Resolver resolverObj = resolverFactory.getResolver(jso.getString("type"));
                    if (resolverObj == null){
                        continue;
                    }

                    resolverObj.init(jso);

                    // 放入缓存
                    String key = name + "." +resolverObj.getId();
                    ResolversHandler.put(key, resolverObj);
                }

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (fileReader != null){
                    try {
                        fileReader.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

    }

    public static void main(String[] args) {
        File f = new File(ResolverReader.class.getResource("/").getPath() + "/exsolverMapper");
        System.out.println(Normalizer.normalize("classpath*:/exsolverMapper",Normalizer.Form.NFKC));
    }

}
